﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PavilionGame;
using Pavilion.Communications;

namespace Pavilion.DesignerTool
{
    public class TextureTransfer
    {
        static public void Update(TextureData textureData)
        {
            AnimationData animationDataParent = textureData.Parent.Parent;
            ResourceData resourceDataParent = animationDataParent.Parent;

            var frameIndex = animationDataParent.FrameDataCollection.IndexOf(textureData.Parent);
            var textureIndex = textureData.Parent.TextureDataCollection.IndexOf(textureData);

            Update(resourceDataParent.ResourceName, animationDataParent.Name, frameIndex, textureIndex, textureData.Texture);
        }

        static public void Update(string resourceName, string animationName, int frameIndex, int textureIndex, CTexture2D texture)
        {
            var textureWidth = texture.Width;
            var textureHeight = texture.Height;
            var textureData = new byte[textureWidth * textureHeight * 4];
            texture.TextureItem.GetData<byte>(textureData);

            DataTransferClient.Instance.TransferData((proxy) => proxy.UpdateTexture(resourceName, animationName, frameIndex, textureIndex, textureData, textureWidth, textureHeight));
        }

        static public void Insert(FrameData frameData, CTexture2D texture, int textureIndex)
        {
            ResourceData resourceDataParent = frameData.Parent.Parent;
            AnimationData animationDataParent = frameData.Parent;
            var frameIndex = animationDataParent.FrameDataCollection.IndexOf(frameData);

            Insert(resourceDataParent.ResourceName, animationDataParent.Name, frameIndex, textureIndex, texture);
        }

        static public void Insert(string resourceName, string animationName, int frameIndex, int textureIndex, CTexture2D texture)
        {
            var textureWidth = texture.Width;
            var textureHeight = texture.Height;
            var textureData = new byte[textureWidth * textureHeight * 4];
            texture.TextureItem.GetData<byte>(textureData);

            DataTransferClient.Instance.TransferData((proxy) => proxy.InsertTexture(resourceName, animationName, frameIndex, textureIndex, textureData, textureWidth, textureHeight));
            UpdateVisibility(resourceName, animationName, frameIndex, textureIndex, texture.Visible);
        }

        static public void Reorder(FrameData frameData, int oldIndex, int newIndex)
        {
            ResourceData resourceDataParent = frameData.Parent.Parent;
            AnimationData animationDataParent = frameData.Parent;
            var frameIndex = animationDataParent.FrameDataCollection.IndexOf(frameData);

            Reorder(resourceDataParent.ResourceName, animationDataParent.Name, frameIndex, oldIndex, newIndex);
        }

        static public void Reorder(string resourceName, string animationName, int frameIndex, int oldIndex, int newIndex)
        {
            DataTransferClient.Instance.TransferData((proxy) => proxy.ReorderTexture(resourceName, animationName, frameIndex, oldIndex, newIndex));
        }

        static public void Remove(FrameData frameData, int index)
        {
            ResourceData resourceDataParent = frameData.Parent.Parent;
            AnimationData animationDataParent = frameData.Parent;
            var frameIndex = animationDataParent.FrameDataCollection.IndexOf(frameData);

            Remove(resourceDataParent.ResourceName, animationDataParent.Name, frameIndex, index);
        }

        static public void Remove(string resourceName, string animationName, int frameIndex, int index)
        {
            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveTexture(resourceName, animationName, frameIndex, index));
        }

        static public void UpdateVisibility(TextureData textureData)
        {
            AnimationData animationDataParent = textureData.Parent.Parent;
            ResourceData animationResourceDataParent = animationDataParent.Parent;

            var frameIndex = animationDataParent.FrameDataCollection.IndexOf(textureData.Parent);
            var textureIndex = textureData.Parent.TextureDataCollection.IndexOf(textureData);

            UpdateVisibility(animationResourceDataParent.ResourceName, animationDataParent.Name, frameIndex, textureIndex, textureData.Visible);
        }

        static public void UpdateVisibility(string resourceName, string animationName, int frameIndex, int textureIndex, bool isVisible)
        {
            DataTransferClient.Instance.TransferData((proxy) => proxy.UpdateTextureVisibility(resourceName, animationName, frameIndex, textureIndex,isVisible));
        }
    }
}
